package cn.mw.cmdb.param;

import cn.mw.cmdb.entity.FieldViewAuthType;
import cn.mw.cmdb.entity.InstanceInfo;
import cn.mw.cmdb.util.CMDBTool;
import cn.mw.components.mongodb.entity.SearchParam;
import cn.mw.components.mongodb.entity.SubCondition;
import com.google.common.base.Strings;
import lombok.Data;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static cn.mw.cmdb.enums.ZabbixAssetsStatusEnum.ALERT;
import static cn.mw.cmdb.enums.ZabbixAssetsStatusEnum.NORMAL;
import static cn.mw.cmdb.service.impl.TableViewManageServiceImpl.itemAssetsStatusKey;
import static cn.mw.cmdb.util.ValConvertUtil.strValueConvert;
import static cn.mw.components.mongodb.entity.CriteriaOpsType.*;

@Data
public class TableSearchParam extends SearchParam implements SearchParamConvert {
    private String tableId;
    private String id;
    private String pId;
    private String type;
    private String menuId;
    private String instanceId;
    private String financeParentId;
    private List<String> instanceIds;
    private String viewType;
    private FieldViewAuthType authType;
    private String path;
    private String groupName;
    //是否隐藏资产状态
    private Boolean hideStatusFlag;
    //是否首页资产状态跳转查询
    private boolean enableHomePageStatusLink;
    //是否查询监控状态
    private boolean enableMonitorStatus;

    private Map<String, Object> data;

    private List<String> showTypeList;
    //模型视图类型(defaultView, roomView, cabinetView, UNumView)
    private String modelViewType;

    //是否展示所有字段
    private boolean isShowAllField;

    private String itemAssetsStatus;

    private List<String> interceptFilterList = Arrays.asList(itemAssetsStatusKey);

    public String getConvertId() {
        String convertId = id;
        if ((!Strings.isNullOrEmpty(id)) && (id.contains(","))) {
            convertId = id.split(",")[0];
        }
        return convertId;
    }


    @Override
    public void convert() {
        if (!Strings.isNullOrEmpty(path)) {
            this.addSubCondition(CMDBTool.getFieldName(InstanceInfo::getPath)
                    , new SubCondition(in.name(), path));
        }
        if (getCondition() != null) {
            Map<String, SubCondition> map = getCondition();
            map.forEach((k, v) -> {
                MwKeyValueParam param = new MwKeyValueParam();
                String key = k;
                SubCondition subCondition = v;
                Object value = subCondition.getValue();
                String operation = subCondition.getOperation();
                //特殊处理，前端查询资产状态：告警时，默认设置为正常，从正常状态中细分出告警
                if (itemAssetsStatusKey.equals(key) && like.name().equals(operation) && ALERT.getName().contains(strValueConvert(value))) {
                    subCondition.setValue(NORMAL.getName());
                }
            });
        }
    }

    public List<MwKeyValueParam> getQueryKeyValue() {
        List<MwKeyValueParam> list = new ArrayList<>();
        if (getCondition() != null) {
            Map<String, SubCondition> map = getCondition();
            map.forEach((k, v) -> {
                if(interceptFilterList.contains(k)){
                    MwKeyValueParam param = new MwKeyValueParam();
                    String key = k;
                    SubCondition subCondition = v;
                    Object value = subCondition.getValue();
                    String operation = subCondition.getOperation();
                    if ((is.name().equals(operation) || like.name().equals(operation)) && !Strings.isNullOrEmpty(strValueConvert(value))) {
                        param.setName(key);
                        param.setValue(strValueConvert(value));
                        list.add(param);
                    }
                }
            });
        }
        return list;
    }
}
